import EventConfig from "../data/EventConfig";
import Kits from "../kit/Kits";
import UserDogModel from "../model/UserDogModel";
import EventName from "./EventName";
import UserData from "./UserData";

export default class OperateData {

    //是否首次登录
    public static isFirstLogin() {
        return UserData.getInstance().onLineTime == 0;
    }

    //出生
    public static beginBirth(data) {
        UserData.getInstance().birthData = new UserDogModel(data);
        cc.director.getScene().emit(EventName.ADD_AGE)
    }

    //增加年龄
    public static addAge() {
        UserData.getInstance().birthData.addAge();
        cc.director.getScene().emit(EventName.ADD_AGE)
    }

    //获取天气
    public static getWeatherDataByAge(age) {
        if (!UserData.getInstance().weatherDataList[age]) {
            UserData.getInstance().weatherDataList[age] = this.randomWeatherData();
        }
        return UserData.getInstance().weatherDataList[age];
    }

    //随机天气
    public static randomWeatherData() {
        let age = UserData.getInstance().birthData.age;
        //根据经过的时间，来随机的一个天气
        //todo
        let data = Kits.randomInter(0, 21);
        return data;
    }

    //获取事件
    public static getEventItemId() {
        //记录了发生哪些事件
        let record = UserData.getInstance().eventRecord;
        let eventList = EventConfig.data;
        let age = UserData.getInstance().birthData.age;

        let list = [];

        for (let id in eventList) {
            let item = eventList[id];
            //找一个符合该年龄发生的事件
            if (item.range[0] <= age && item.range[1] >= age) {
                //没有发生过的事件
                if (!record.includes(id)) {
                    list.push(id);
                }
            }
        }

        let randomInter = Kits.randomInter(0, list.length - 1);
        return list[randomInter];
    }

    //做出选择
    public static chooseBehavior(eventId, chooseIndex) {
        UserData.getInstance().chooseRecord[UserData.getInstance().birthData.age] = { id: eventId, index: chooseIndex };
    }

    //获取描述
    public static getDesByAge(age) {
        let record = UserData.getInstance().chooseRecord[age];
        let index = record.index;
        let eventItem = this.getRecordEventItemByAge(age);
        let behaviorList = eventItem.behaviorList;
        let behavior = behaviorList[index].behavior;
        return behavior;
    }

    //获取记录的对应每个年龄发生的事件
    public static getRecordEventItemByAge(age) {
        let record = UserData.getInstance().chooseRecord[age];
        let id = record.id;
        let eventItem = this.getEventItemByid(id);
        return eventItem;
    }

    //通过id获取事件
    public static getEventItemByid(id) {
        let eventItem = EventConfig.data[id];
        return eventItem;
    }
}
